package order

import (
	"encoding/json"
	"log"
	"parking-lot-go-push/model"
	"time"
)

type OrderExcess struct {
	Id           int       `json:"id" gorm:"type:int(11) auto_increment; NOT NULL; primary_key"`
	OrderType    int       `json:"order_type" gorm:"type:tinyint(1);  COMMENT:'1小强 2泊安飞'"`
	ParkCode     string    `json:"park_code" gorm:"type:varchar(20); COMMENT:'停车场编号'"`
	OutNumber    int       `json:"out_number" gorm:"type:int(11); COMMENT:'车辆数'"`
	StartTime    int       `json:"start_time" gorm:"type:int(11); COMMENT:'开始时间戳'"`
	EndTime      int       `json:"end_time" gorm:"type:int(11); COMMENT:'结束时间戳'"`
	Status       int       `json:"status" gorm:"type:tinyint(1); COMMENT:'状态 1进行中 2已结束'"`
	CarNumbers   string    `json:"car_numbers" gorm:"type:text; COMMENT:'车牌号'"`
	NormalNumber int       `json:"-"`
	CreatedAt    time.Time `json:"created_at" gorm:"type:datetime"`
	UpdatedAt    time.Time `json:"updated_at" gorm:"type:datetime"`
}

func (c *OrderExcess) TableName() string {
	return "order_excess"
}

// 记录超停记录
func (c *OrderExcess) Excess() error {
	var orderExcess OrderExcess
	//查询当前超停记录是否存在
	exists, err := model.GetModelDataFirst(
		model.Db,
		c.TableName(),
		&orderExcess,
		model.QueryOptions{
			Conditions: map[string]interface{}{
				"park_code":  c.ParkCode,
				"out_number": c.OutNumber,
				"status":     1,
				"order_type": c.OrderType,
			},
		})

	if err != nil {
		return err
	}

	//没有记录
	if !exists {
		//停止现有记录
		if err := model.UpdateModelData(
			model.Db,
			c.TableName(),
			map[string]interface{}{
				"park_code":  c.ParkCode,
				"status":     1,
				"order_type": c.OrderType,
			},
			map[string]interface{}{
				"end_time": time.Now().Unix(),
				"status":   2,
			},
		); err != nil {
			return err
		}

		var orderData OrderStruct
		var orderList []OrderStruct

		model.GetModelData(
			model.Db,
			orderData.TableName(),
			&orderList,
			model.QueryOptions{
				Conditions: map[string]interface{}{
					"static_park_code": c.ParkCode,
					"status":           1,
					"order_type":       c.OrderType,
				},
				OrderBy: "in_time asc",
			})

		var carNumbers []string
		for k, v := range orderList {
			//跳过符合要求的车牌
			if k+1 <= c.NormalNumber {
				continue
			}
			carNumbers = append(carNumbers, v.CarNumber)
		}

		carNumbersJson, err := json.Marshal(carNumbers)
		if err != nil {
			log.Fatal(err)
		}

		//新增超停记录
		if err := model.CreateModelData(
			model.Db,
			c.TableName(),
			map[string]interface{}{
				"park_code":   c.ParkCode,
				"out_number":  c.OutNumber,
				"start_time":  time.Now().Unix(),
				"status":      1,
				"order_type":  c.OrderType,
				"car_numbers": string(carNumbersJson),
			},
		); err != nil {
			return err
		}
	}

	return nil
}

// 停止超停记录
func (c *OrderExcess) StopExcess() error {
	//停止现有记录
	if err := model.UpdateModelData(
		model.Db,
		c.TableName(),
		map[string]interface{}{
			"park_code":  c.ParkCode,
			"status":     1,
			"order_type": c.OrderType,
		},
		map[string]interface{}{
			"end_time": time.Now().Unix(),
			"status":   2,
		},
	); err != nil {
		return err
	}
	return nil
}
